/**
 * JAVASCRIPT
 * 
 * @author qiuminggang
 * @date 2012/05/18
 * @version 1.0
 * @description 流量统计
 * @history <author><time><version><desc>
 */

(function() {
	var d = document, w = window, n = navigator, l = location, m = function() {
		this.className = "STATISTICS";
		this.NR = "http://115.238.237.226:5705/getData";
		this.init();
	};

	m.prototype = {
		init : function() {
			this.date = new Date;
			this.now = parseInt(this.date.getTime());
			this.date.setTime(this.now);
		},
		getAll : function() {
			this.getReferer();
			this.getPath();
			this.getPageName();
			//this.getSys();
			this.getBrowser();
			this.getLG();
			this.getScreenRes();
			this.gePlugins();
			this.getFlashVersion();
			this.getSysType();
			this.getPlatform();
		},
		getReferer : function() {
			this.refer = d.referrer || "";
			// this.refer = this.refer;
		},
		getPath : function() {
			// this.path = l.pathname;
			this.path = d.URL;
		},
		getPageName : function() {
			this.href = l.href;
			this.pageName = this.href.slice(this.href.lastIndexOf("/") + 1);
		},
		getSysType:function()
		{
			  var sUserAgent = navigator.userAgent; 
			  var isWin = (navigator.platform == "Win32") || (navigator.platform == "Windows");   
			  var isMac = (navigator.platform == "Mac68K") || (navigator.platform == "MacPPC") || (navigator.platform == "Macintosh") || (navigator.platform == "MacIntel");
			  var isLinux = (String(navigator.platform).indexOf("Linux") > -1);   
			  var isUnix = (navigator.platform == "X11") && !isWin && !isMac;
			  var isWin2K = sUserAgent.indexOf("Windows NT 5.0") > -1 || sUserAgent.indexOf("Windows 2000") > -1; 
			  if (isMac) this.sysType = "Mac";   
			  else if (isUnix) this.sysType = "Unix";   
			  else if (isLinux) this.sysType = "Linux";   
			  else if (isWin) {   
			  if (isWin2K) this.sysType = "Win2000";   
			  var isWinXP = sUserAgent.indexOf("Windows NT 5.1") > -1 ||  
			  sUserAgent.indexOf("Windows XP") > -1;   
			  if (isWinXP) this.sysType = "WinXP";   
			  var isWin2003 = sUserAgent.indexOf("Windows NT 5.2") > -1 || sUserAgent.indexOf("Windows 2003") > -1;   
			  if (isWin2003) this.sysType = "Win2003";   
			  var isWinVista= sUserAgent.indexOf("Windows NT 6.0") > -1 || sUserAgent.indexOf("Windows Vista") > -1;   
			  if (isWinVista) this.sysType = "WinVista";   
			  var isWin7 = sUserAgent.indexOf("Windows NT 6.1") > -1 || sUserAgent.indexOf("Windows 7") > -1;   
			  if (isWin7) this.sysType = "Win7";   
			  }
			  else
			  {
				 this.sysType = "other";
			  }
		},
		getBrowser : function() {
			var fAppVersion = parseFloat(navigator.appVersion);
			// 比较浏览器版本大小
			function compareVersions(sVersion1, sVersion2) {
				var aVersion1 = sVersion1.split(".");
				var aVersion2 = sVersion2.split(".");

				if (aVersion1.length > aVersion2.length) {
					for ( var i = 0; i < aVersion1.length; i++)
						aVersion2.push("0");
				} else if (aVersion1.length < aVersion2.length) {
					for ( var i = 0; i < aVersion2.length; i++)
						aVersion1.push("0");
				}

				for ( var i = 0; i < aVersion1.length; i++) {
					if (aVersion1[i] < aVersion2[i])
						return -1;
					else if (aVersion1[i] > aVersion2[i])
						return 1;
				}
				return 0;
			}
			// 检测是否Opera浏览器
			var sUserAgent = navigator.userAgent;
			var isOpera = sUserAgent.indexOf("Opera") > -1;
			// var isMinOpera4 = isMinOpera5 = isMinOpera6 = isMinOpera7 =
			// isMinOpera7_5 = false;

			if (isOpera) {
				var fOperaVersion;
				if (navigator.appName == "Opera") {
					fOperaVersion = fAppVersion;
				} else {
					var reOperaVersion = new RegExp("Opera (\d+\.\d+)");
					reOperaVersion.test(sUserAgent);
					fOperaVersion = parseFloat(RegExp["$1"]);
				}

				this.browser = "Opera" + fOperaVersion;
			}
			// 检测是否Safari
			var isKHTML = sUserAgent.indexOf("KHTML") > -1
					|| sUserAgent.indexOf("Konqueror") > -1
					|| sUserAgent.indexOf("AppleWebKit") > -1;

			// var isMinSafari1 = isMinSafari1_2 = false;
			// var isMinKonq2_2 = isMinKonq3 = isMinKonq3_1 = isMinKonq3_2 =
			// false;
			if (isKHTML) {
				isSafari = sUserAgent.indexOf("AppleWebKit") > -1;
				isKonq = sUserAgent.indexOf("Konqueror") > -1;
				if (isSafari) {
					var isChrome = sUserAgent.indexOf("Chrome") > -1;
					this.browser = isChrome ? "Chrome" : "Safari";
				} else if (isKonq) {
					this.browser = "Konq";
				}
			}
			// 检测IE
			var isIE = sUserAgent.indexOf("compatible") > -1
					&& sUserAgent.indexOf("MSIE") > -1 && !isOpera;

			// var isMinIE4 = isMinIE5 = isMinIE5_5 = isMinIE6 = isMinIE7 =
			// false;
			if (isIE) {
				/MSIE (\d+\.\d+);/.test(sUserAgent);
				var fIEVersion = parseFloat(RegExp["$1"]);

				this.browser = "IE" + fIEVersion;
			}
			// 检测Mozilla
			var isMoz = sUserAgent.indexOf("Gecko") > -1 && !isKHTML;
			// var isMinMoz1 = isMinMoz1_4 = isMinMoz1_5 = isMinMoz1_8 = false;
			if (isMoz) {
				// 检测Firfox
				var isFF = sUserAgent.indexOf("Firefox") > -1 && !isKHTML;
				// var isMinFF1 = isMinFF2 =isMinFF3 = false;
				if (isFF) {
					/Firefox\/(\d+\.\d+)/.test(sUserAgent);
					var fffVersion = parseFloat(RegExp["$1"]);
					this.browser = "Firefox" + fffVersion;
				} else {
					/rv:(\d+\.\d+)/.test(sUserAgent);
					var fMOZVersion = parseFloat(RegExp["$1"]);
					this.browser = "Mozilla" + fMOZVersion;
				}
			}
			var isNS4 = !isIE && !isOpera && !isMoz && !isKHTML
					&& (sUserAgent.indexOf("Mozilla") == 0)
					&& (navigator.appName == "Netscape")
					&& (fAppVersion >= 4.0 && fAppVersion < 5.0);
			var isMinNS4 = isMinNS4_5 = isMinNS4_7 = isMinNS4_8 = false;

			if (isNS4) {
				isMinNS4 = true;
				this.browser = "Netscape" + fAppVersion;
			}
		},
		getLG : function() {
			this.lg = n.systemLanguage || n.language;
			this.lg = this.lg.toLowerCase();
		},
		getScreenRes : function() {
			this.px = screen.pixelDepth;
			this.screenRes = screen.width + '*' + screen.height;
		},
		gePlugins : function() {
			// 插件数目
			this.pluginsNum = n.plugins.length;

			var arr = [];

			for ( var ii = 0; ii < this.pluginsNum; ii++) {
				var name = n.plugins[ii].name;
				var fileName = n.plugins[ii].filename;
				arr.push(name + ":" + fileName);
			}

			this.plugins = arr.join(",");
		},
		getFlashVersion : function() {
			var f = "-";
			if (n.plugins && n.plugins.length) {
				for ( var ii = 0; ii < n.plugins.length; ii++) {
					if (n.plugins[ii].name.indexOf('Shockwave Flash') != -1) {
						f = n.plugins[ii].description.split('Shockwave Flash')[1];
						break;
					}
				}
			} else if (w.ActiveXObject) {
				for ( var ii = 10; ii >= 2; ii--) {
					try {
						var fl = eval("new ActiveXObject('ShockwaveFlah.ShockwaveFlash."
								+ ii + "');");
						if (fl) {
							f = ii + '.0';
							break;
						}
					} catch (e) {
						f = "";
					}
				}
			}
			this.flash = f;
		},
		getPlatform : function(){
			this.platform = n.platform;
		},
		requestNext : function() {

			this.getAll();
			var jsonData = {
				'time' : this.now,
				'refer' : escape(this.refer),
				'path' : escape(this.path),
				'page' : escape(this.pageName),
				'os' : escape(this.sysType),
				'browser' : this.browser,
				'lg' : this.lg,
				'screenres' : this.screenRes,
				'plugins' : this.plugins,
				'flash' : this.flash,
				'firVisit' : this.firVisit,
				'platform' : this.platform
			};
			// alert(jsonData);
			var a = this.NR + "?data=" + JSON.stringify(jsonData);
			// alert(a);
			document.write("<img src='" + a + "' width=0 height=0 border=0 />");
		}

	};

	var g = new m();
	window.ygy = g;
	w[g.className] = g;

	try {
		STATISTICS.requestNext();
	} catch (error) {
	}

})();